package com.zsl.swing.redis.desktop.window.dialog;

import com.zsl.swing.redis.desktop.common.IconPaths;
import com.zsl.swing.redis.desktop.model.FontSizeConfigEntity;
import com.zsl.swing.redis.desktop.type.BtnOptEnum;
import com.zsl.swing.redis.desktop.type.MainMenuEnum;
import com.zsl.swing.redis.desktop.utils.*;
import com.zsl.swing.redis.desktop.window.ZslRedisDesktopMainWindow;
import com.zsl.swing.redis.desktop.window.builder.ConfirmCancelBtnPanelBuilder;
import com.zsl.swing.redis.desktop.window.builder.FontNameComboBoxBuilder;
import com.zsl.swing.redis.desktop.window.ext.JIntegerField;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;

public class FontSizeSetDialog extends JDialog {

    private int width = 800;

    private int height = 600;

    private JComboBox<String> fontFamilyNameComboBox = FontNameComboBoxBuilder.getInstance();

    private JIntegerField windowField = new JIntegerField(COLUMNS);

    private JIntegerField menuField = new JIntegerField(COLUMNS);

    private JIntegerField treeField = new JIntegerField(COLUMNS);

    private JIntegerField cmdAreaField = new JIntegerField(COLUMNS);

    private JIntegerField logAreaField = new JIntegerField(COLUMNS);

    private static final int COLUMNS = 100;

    private static final Image SET_ICON = IconUtils.getScaleImage(IconPaths.SET_ICON, 30, 30);


    public static void showDialog(){
        new FontSizeSetDialog();
    }
    private FontSizeSetDialog(){
        super(ZslRedisDesktopMainWindow.getMainWindow());

        this.setTitle(MainMenuEnum.FONT_SIZE_SET.getText());
        this.setIconImage(SET_ICON);
        this.setSize(this.width, this.height);

        int x = CommonUtils.maxWidth()/2 - this.width/2;
        int y = CommonUtils.maxHeight()/2 - this.height/2;
        this.setLocation(x, y);

        this.initDialog();

        this.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
        this.setVisible(true);
    }

    private void initDialog() {
        JPanel propPanel = this.initPropertyPanel();
        JPanel btnsPanel = this.initBtnPanel();

        Box box = Box.createVerticalBox();
        box.add(propPanel);
        box.add(btnsPanel);

        this.setContentPane(box);
    }

    private JPanel initBtnPanel() {
        ActionListener actionListener = event -> {
            String command = event.getActionCommand();
            if(BtnOptEnum.CANCEL.getText().equals(command)){
                this.dispose();
            }else if(BtnOptEnum.CONFIRM.getText().equals(command)){
                this.saveFontConfig();
            }
        };

        return ConfirmCancelBtnPanelBuilder.getInstance(actionListener);
    }

    private FontSizeConfigEntity getEntity(){
        FontSizeConfigEntity entity = new FontSizeConfigEntity();
        entity.setFontName((String) this.fontFamilyNameComboBox.getSelectedItem());
        entity.setWindow(this.windowField.getInteger());
        entity.setMenu(this.menuField.getInteger());
        entity.setTree(this.treeField.getInteger());
        entity.setCmdArea(this.cmdAreaField.getInteger());
        entity.setLogArea(this.logAreaField.getInteger());

        return entity;
    }

    private void setEntity(){
        FontSizeConfigEntity fontSizeConfig = ZslRedisDesktopMainWindow.getFontSizeConfig();
        this.fontFamilyNameComboBox.setSelectedItem(fontSizeConfig.getFontName());
        this.windowField.setInteger(fontSizeConfig.getWindow());
        this.menuField.setInteger(fontSizeConfig.getMenu());
        this.treeField.setInteger(fontSizeConfig.getTree());
        this.cmdAreaField.setInteger(fontSizeConfig.getCmdArea());
        this.logAreaField.setInteger(fontSizeConfig.getLogArea());
    }

    private void saveFontConfig() {
        FontSizeConfigEntity entity = this.getEntity();

        FontSizeConfigEntity fontSizeConfig = ZslRedisDesktopMainWindow.getFontSizeConfig();
        if(StringUtils.isEmpty(entity.getFontName())){
            entity.setFontName(fontSizeConfig.getFontName());
        }

        if(entity.getWindow() <= 0){
            entity.setWindow(fontSizeConfig.getWindow());
        }

        if(entity.getMenu() <=0 ){
            entity.setMenu(fontSizeConfig.getMenu());
        }

        if(entity.getTree() <= 0){
            entity.setTree(fontSizeConfig.getTree());
        }

        if(entity.getCmdArea() <= 0){
            entity.setCmdArea(fontSizeConfig.getCmdArea());
        }

        if(entity.getLogArea() <= 0){
            entity.setLogArea(fontSizeConfig.getLogArea());
        }

        boolean result = ConfigUtils.writeFontSizeConfig(entity);
        if(result){
            this.dispose();
            DialogUtils.restartConfirmDialog(ZslRedisDesktopMainWindow.getMainWindow(), "保存设置成功，重启后生效！");
        }else{
            DialogUtils.errorDialog(ZslRedisDesktopMainWindow.getMainWindow(),"保存设置失败！");
        }
    }

    private boolean isValidFontSize(String fontSizeStr){
        return StringUtils.isInt(fontSizeStr) && fontSizeStr.trim().compareTo("0") > 0;
    }

    private JPanel initPropertyPanel() {
        JPanel panel = new JPanel(new GridBagLayout());

        int start = 0,labelWidth = 40, height = 10, fieldWidth = 80, seperator = 5;
        GridBagUtils.getInstance()
                .add(new JLabel("字体"),panel, start, start, labelWidth,height)
                .add(this.fontFamilyNameComboBox,panel,labelWidth + seperator, start, fieldWidth, height)
                .add(new JLabel("窗口"), panel, start, height + seperator, labelWidth, height)
                .add(this.windowField, panel, labelWidth + seperator, height + seperator, fieldWidth, height)
                .add(new JLabel("菜单"), panel, start, 2*(height + seperator), labelWidth, height)
                .add(this.menuField, panel, labelWidth + seperator, 2*(height + seperator), fieldWidth, height)
                .add(new JLabel("树"), panel, start, 3*(height + seperator), labelWidth, height)
                .add(this.treeField, panel, labelWidth + seperator, 3*(height + seperator), fieldWidth, height)
                .add(new JLabel("控制台"), panel, start, 4*(height + seperator), labelWidth, height)
                .add(this.cmdAreaField, panel, labelWidth + seperator, 4*(height + seperator), fieldWidth, height)
                .add(new JLabel("日志"), panel, start, 5*(height + seperator), labelWidth, height)
                .add(this.logAreaField, panel, labelWidth + seperator, 5*(height + seperator), fieldWidth, height);

        this.setEntity();

        this.fontFamilyNameComboBox.setEditable(false);
        return panel;
    }


}
